package com.prs.crm.service.oa.impl;

import java.io.File;
import java.util.Collection;
import java.util.Date;

import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.AbstractDataset;
import org.jfree.data.general.DefaultPieDataset;

import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.oa.KnowledgeDao;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.oa.Knowledge;
import com.prs.crm.domain.oa.KnowledgeType;
import com.prs.crm.service.impl.FlowServiceImpl;
import com.prs.crm.service.oa.KnowledgeService;

public class KnowledgeServiceImpl extends FlowServiceImpl<Knowledge,KnowledgeDao> implements KnowledgeService{

	
	@Override
	public boolean valideSubmit(Knowledge entity)
	{
		if (entity!=null 
				&& entity.getId()!=null && entity.getFlowstatus()!=Constrains.WORKFLOWENTITY_STATE_PROCESS
				&& entity.getFlowstatus()!=Constrains.WORKFLOWENTITY_STATE_INVALID)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	public void save(Knowledge knowledge,File attach,String attachName)
	{
		if (attach!=null)
		{
			Attach at = FileUtil.getInstance().saveFile(attach, attachName, "", Constrains.UPLOAD_DIR_KNOWLEDGE);
			Attachment atm = new Attachment();
			atm.setPath(at.getSrc());
			atm.setName(attachName);
			if (knowledge.getAttachment()!=null)
				knowledge.setAttachment(null);
			knowledge.setAttachment(atm);
		}
		super.save(knowledge);
		//return this.submitFlow(knowledge,instance);//提交流程
		
	}

	@Override
	public Collection<Knowledge> getAllKnowledge() {
		// TODO Auto-generated method stub
		return this.getDao().getAllKnowledge();
	}

	@Override
	public Collection<Knowledge> getKnowledgeByIds(Integer[] ids) {
		// TODO Auto-generated method stub
		return this.getDao().getKnowledgeByIds(ToolKits.convertToIntAry(ids));
	}

	@Override
	public Collection<?> getReportKnowledgeSource(Date datestart, Date dateend) {
		// TODO Auto-generated method stub
		return this.getDao().getReportKnowledgeSource(datestart, dateend);
	}
	@SuppressWarnings("unchecked")
	@Override
	public AbstractDataset fillDataset(Collection<?> dataList, int GraphicsType) {
		// TODO Auto-generated method stub
		AbstractDataset Dataset = null;
		if(GraphicsType == Constrains.REPORT_PIECHART)
			Dataset = new DefaultPieDataset();
		else if(GraphicsType == Constrains.REPORT_BARCHART)
			Dataset = new DefaultCategoryDataset();
		
		for(Object[] o : (Collection<Object[]>)dataList){
			String name = "";
			Number count = 0;
			for(Object value : o){
				if(value != null){
					if(value instanceof KnowledgeType){
						name = ((KnowledgeType) value).getName();
					}else if(value instanceof Number) {
						count = (Number)value;
					}
				}
			}
			if(!name.equals("")){
				if(GraphicsType == Constrains.REPORT_PIECHART)
					((DefaultPieDataset)Dataset).setValue(name, count);
				else if(GraphicsType == Constrains.REPORT_BARCHART)
					((DefaultCategoryDataset)Dataset).addValue(count, name, name);
			}
		}
		return Dataset;
	}

}
